Skip to main content

Calling contract functions

Interacting

In this chapter, we will not only learn how to send TLOS tokens using the Telos Cloud Wallet but also how to execute any function from any contract on the Telos EVM network.

To explain this, we will break down the procedure into three parts. The last two parts are always the same, but the first one defines which contract and method you will be calling, along with the value of TLOS sent.

info

The following information is an extraction from the OreIdAuth class which implements all examples presented in this doc.

STEP 1: Creating Transaction Body

Based on the transaction type you intend to execute, you first need to create its body. Here are configurations for several common transaction types:

System Token Transfer

This is a basic token TLOS transfer within the system:

const systemTransactionBody = {
from, // sender's address
to, // recipient's address
value, // amount to transfer, use ethers.BigNumber.toHexString() for conversion
};

ERC20 Token Transfer

To transfer ERC20 tokens:

const erc20TransactionBody = {
from, // sender's address
to: token_address, // the contract address of the ERC20 token
contract: {
abi, // ABI of the ERC20 token contract
parameters: [to, value], // parameters (destination, amount sent)
method: 'transfer', // method name for the ERC20 transfer
},
} as unknown as JSONObject;

Wrap System Token into ERC20 Token

If you want to wrap your system token TLOS into an ERC20 token WTLOS:

const wrapTransactionBody = {
from, // sender's address
to, // contract address of the wrapped system token
value, // amount to wrap
contract: {
abi, // ABI of the contract
parameters: [],
method: 'deposit', // method name for depositing
},
} as unknown as JSONObject;

Unwrap ERC20 Back to System Token

To convert your wrapped ERC20 token back into a system token:

const unwrapTransactionBody = {
from, // sender's address
to, // contract address of the wrapped system token
contract: {
abi, // ABI of the contract
parameters: [value], // value to unwrap, use ethers.BigNumber.toHexString() for conversion
method: 'withdraw', // method name for withdrawing
},
} as unknown as JSONObject;

STEP 2: Constructing the Transaction

With the transaction body configured, the next step is to construct the final transaction structure:

const transaction = await oreId.createTransaction({
transaction: transactionBody, // use one of the previously created transaction bodies
chainAccount: from, // sender's blockchain account address
chainNetwork: ChainNetwork.TelosEvmTest, // or ChainNetwork.TelosEvmMain
signOptions: {
broadcast: true, // to effectively send the transaction
returnSignedTransaction: true, // to get a response after transaction execution
},
});

STEP 3: Signing and Broadcasting the Transaction

After constructing the transaction, prompt the user to sign it using a popup:

const { transactionId } = await oreId.popup.sign({ transaction });
console.log('transaction hash:', transactionId);

The user will see a popup window where they can either confirm or reject the transaction.

Conclusion

With oreid-js, you can seamlessly integrate blockchain transactions into your web or mobile application. By abstracting away the complexities, ORE ID ensures a smooth user experience while maintaining the security and integrity of blockchain transactions.